/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */
package org.baatar.remoting.test;

import org.baatar.remoting.test.impl.client.MathServiceClient;

/**
 * Main program of the remote math service client which randomly calls remote
 * methods from remote math service.
 * 
 * @author B_ZEREN
 */
public class RemotingMathClient {
    public static void main(String args[]) {
        final String hostName = "127.0.0.1";
        final int portNo = 4321;
        final String clientInfo = "MathRemoteClient";

        Thread caller = new Thread(new Runnable() {

            @Override
            public void run() {
                MathServiceClient client = new MathServiceClient();
                client.setHostName(hostName);
                client.setPortNo(portNo);
                client.setCallerInfo(clientInfo);

                while (true) {

                    double r = Math.random();

                    double a = Math.random();
                    double b = Math.random();
                    double res = 0.0;

                    String operation = null;

                    try {
                        if (r > 0.75) {
                            res = client.add(a, b);
                            operation = "+";
                        } else
                            if (r > 0.5) {
                                res = client.substract(a, b);
                                operation = "-";
                            } else
                                if (r > 0.25) {
                                    res = client.multiply(a, b);
                                    operation = "*";
                                } else {
                                    res = client.divide(a, b);
                                    operation = "/";
                                }

                        System.out.println(a + " " + operation + " " + b + " = " + res);

                        Thread.sleep(100);
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }

                }
            }
        });

        caller.start();
    }
}
